#include "InetAddress.h"

InetAddress::InetAddress(const sockaddr& addr)
{	
	addr_ = *(sockaddr_in*)(&addr);
	char buf[64] = { 0 };
	if (!::inet_ntop(AF_INET, &addr_.sin_addr, buf, sizeof(buf))) {
		perror("inet_ntop");
		assert(false);
	}
    ip_ = buf;
	port_ = ntohs(addr_.sin_port);
}

InetAddress::InetAddress(const std::string& addr, uint16_t port) :
    ip_(addr),
    port_(port)
{
    addr_.sin_family = AF_INET;
    addr_.sin_port = htons(port);
    if (!::inet_pton(AF_INET, addr.c_str(), &addr_.sin_addr))
    {
        perror("inet_pton");
        assert(false);
    }
}

uint16_t InetAddress::port()
{
    return port_;
}

std::string InetAddress::ip()
{
    return ip_;

}
